{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": [
        "# ItemCF推荐Movielen"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "pycharm": {
          "is_executing": false
        }
      },
      "outputs": [],
      "source": "import pandas as pd\nimport numpy as np\nimport scipy.spatial.distance as ssd\nfrom collections import defaultdict"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {
          "name": "#%% md\n"
        }
      },
      "source": "## 读取训练集前三列"
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "outputs": [],
      "source": "df_training_data \u003d pd.read_csv(\u0027./data/movielen_rating_training.base\u0027,usecols\u003d[0, 1, 2], names\u003d[\u0027userid\u0027, \u0027itemid\u0027, \u0027rating\u0027], sep\u003d\u0027\\t\u0027)",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "outputs": [
        {
          "data": {
            "text/plain": "   userid  itemid  rating\n0       1       1       5\n1       1       2       3\n2       1       3       4\n3       1       4       3\n4       1       5       3",
            "text/html": "\u003cdiv\u003e\n\u003cstyle scoped\u003e\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n\u003c/style\u003e\n\u003ctable border\u003d\"1\" class\u003d\"dataframe\"\u003e\n  \u003cthead\u003e\n    \u003ctr style\u003d\"text-align: right;\"\u003e\n      \u003cth\u003e\u003c/th\u003e\n      \u003cth\u003euserid\u003c/th\u003e\n      \u003cth\u003eitemid\u003c/th\u003e\n      \u003cth\u003erating\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003cth\u003e0\u003c/th\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e5\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e1\u003c/th\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e2\u003c/td\u003e\n      \u003ctd\u003e3\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e2\u003c/th\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e3\u003c/td\u003e\n      \u003ctd\u003e4\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e3\u003c/th\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e4\u003c/td\u003e\n      \u003ctd\u003e3\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003cth\u003e4\u003c/th\u003e\n      \u003ctd\u003e1\u003c/td\u003e\n      \u003ctd\u003e5\u003c/td\u003e\n      \u003ctd\u003e3\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/div\u003e"
          },
          "metadata": {},
          "output_type": "execute_result",
          "execution_count": 10
        }
      ],
      "source": "df_training_data.head()",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "## 去重复项,得到userid和itemid列表",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "80000\n943 1650\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "print(len(df_training_data))\nuser_id_s \u003d df_training_data[\u0027userid\u0027].unique()\nitem_id_s \u003d df_training_data[\u0027itemid\u0027].unique()\nprint(len(user_id_s), len(item_id_s))\n# 建立id与index的索引\nuser_index_map \u003d {}\nitem_index_map \u003d {}\nfor user_index in range(len(user_id_s)):\n    user_id \u003d user_id_s[user_index]\n    user_index_map[user_id] \u003d user_index\nfor item_index in range(len(item_id_s)):\n    item_id \u003d item_id_s[item_index]\n    item_index_map[item_id] \u003d item_index",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "## 建立用户与物品的打分矩阵",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "outputs": [
        {
          "data": {
            "text/plain": "array([[0., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.],\n       ...,\n       [0., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.]])"
          },
          "metadata": {},
          "output_type": "execute_result",
          "execution_count": 16
        }
      ],
      "source": "user_item_rating_array \u003d np.zeros(shape\u003d(len(user_id_s), len(item_id_s)))\nuser_item_rating_array",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "outputs": [
        {
          "data": {
            "text/plain": "array([[5., 3., 4., ..., 0., 0., 0.],\n       [4., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.],\n       ...,\n       [5., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.],\n       [0., 5., 0., ..., 0., 0., 0.]])"
          },
          "metadata": {},
          "output_type": "execute_result",
          "execution_count": 22
        }
      ],
      "source": "# 用户打分商品的索引集合，创建默认字典\nuser_rating_map \u003d defaultdict(set)\n# 遍历df_training_data\nfor row_index in df_training_data.index:\n    # 读取每一行数据\n    row_data \u003d df_training_data.iloc[row_index]\n    # print(\u0027row_data\u0027, row_data)\n    # 打分用户的索引\n    user_index \u003d user_index_map[row_data[\u0027userid\u0027]]\n    # 打分电影的索引\n    item_index \u003d item_index_map[row_data[\u0027itemid\u0027]]\n    # print(user_index, item_index)\n    # 添加用户打分商品索引\n    user_rating_map[user_index].add(item_index)\n    # 矩阵中行user_index, 列item_index的元素赋值，也就是打分\n    user_item_rating_array[user_index, item_index] \u003d row_data[\u0027rating\u0027]\nuser_item_rating_array",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "## 计算用户的平均打分向量",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}